package patrol.mobile.com.mobilepatrol.activity.monitor;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.Gson;

import java.util.HashMap;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.EquipPatrolTotal;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil03;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by z on 2018/9/17.
 * Des:机电巡检汇总
 */

public class MechanicalAndElectricalInspectionActivity extends CommonListActivity implements OnRecyclerViewItemClickListener {
    private MySpinner mSpinner;
    EquipPatrolTotal equipPatrolTotal;

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new MyMaeaiAdpter(this);
    }

    @Override
    protected String initTitleName() {
        return getString(R.string.jidianxunjianhuizong);
    }

    @Override
    public void initView() {
        super.initView();
        View view = LayoutInflater.from(this).inflate(R.layout.addview_jdxchz_layout, null);
        mSpinner = view.findViewById(R.id.mSpinner);
        flContent.addView(view);
        mSpinner.setText(TimeUtils.dateStringYM(System.currentTimeMillis()));
        getHttpData(TimeUtils.dateStringYM(System.currentTimeMillis()));
        mSpinner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerUtil03.getInstance().show(MechanicalAndElectricalInspectionActivity.this, new CallBack<DateBean>() {
                    @Override
                    public void onBack(DateBean dateBean) {
//                        currentTime = dateBean.getmDate();
//                        tvStart.setText(dateBean.getmDate());
//                        requestData(currentTime);
                        mSpinner.setText(dateBean.getmDate().substring(0, 7));
                        getHttpData(dateBean.getmDate().substring(0, 7));
                    }
                });
//                DatePickerUtil.getInstance(v.getContext()).setCallBack(new CallBack<DateBean>() {
//                    @Override
//                    public void onBack(DateBean dateBean) {
//                        mSpinner.setText(dateBean.getmDate().substring(0, 7));
//                        getHttpData(dateBean.getmDate().substring(0, 7));
//                    }
//                });
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        mAdapter.setOnItemClickListener(this);
    }


    private void getHttpData(String period) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("orgId", UserManager.getOrgNo());
        hashMap.put("period", period);
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getEquipPatrolTotal);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                equipPatrolTotal = new Gson().fromJson(response, EquipPatrolTotal.class);
                mAdapter.updateItems(equipPatrolTotal.getList());
            }
        });
    }


    @Override
    public void onItemClick(View view, int position) {
        startActivity(new Intent(view.getContext(), JdsbxjwhActivity.class)
                .putExtra("orgId", equipPatrolTotal.getList().get(position).getOrgId()));
    }

    public class MyMaeaiAdpter extends BaseQuickAdapter<EquipPatrolTotal.ListBean> {

        public MyMaeaiAdpter(Context context) {
            super(context);
        }

        @Override
        protected int attachLayoutRes() {
            return R.layout.item_jdxjhz_layout;
        }

        @Override
        protected void convert(BaseViewHolder holder, EquipPatrolTotal.ListBean item) {
            holder.setText(R.id.itemSfzPostion, (equipPatrolTotal.getList().indexOf(item) + 1) + "");
            holder.setText(R.id.itemSfzNzma, item.getOrgName());
            holder.setText(R.id.itemSfzNum, item.getNum() + "条巡检记录");


        }
    }
}
